package com.sihai.ehr.web.action.sign;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AdministrationOutApplyForm;
import com.sihai.ehr.model.biz.AdministrationOutApplyFormAuditing;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.AdministrationOutApplyFormService;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.UserService;
import com.sihai.ehr.utils.DateUtil;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Date;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-15 下午23:58
 * 行政签核系统 - 外出申请单Action
 */
@Results({
        @Result(name = "list", location = "/WEB-INF/pages/sign/administration_out_apply_form_list.jsp"),
        @Result(name = "add", location = "/WEB-INF/pages/sign/administration_out_apply_form_input.jsp"),
        @Result(name = "audit", location = "administration_out_apply_form_audit.jsp")
})
public class AdministrationOutApplyFormAction extends SignBaseAction {
    @BeanInject
    private AdministrationOutApplyFormService administrationOutApplyFormService;
    @BeanInject
    private EmployeeService employeeService;
    @BeanInject
    private UserService userService;

    private LimitedList<AdministrationOutApplyForm> applyForms;
    private AdministrationOutApplyForm applyForm;

    private List<List<AdministrationOutApplyFormAuditing>> audits;

    @Action("/public/administration-out-apply-form")
    public String listByUser() throws Exception {
        userType = 2;
        String temp = execute();
        showChildOrg = false;
        return temp;
    }

    @Action("/sign/administration-out-apply-form")
    @Override
    public String execute() throws Exception {
        showChildOrg = true;
        init();
        User user = null;
        if(userType == 2){
            user = userService.getUser(getCurrentUserId());
        }
        applyForms = administrationOutApplyFormService.findAdministrationOutApplyForm(user, searchFormPojo, createLimit(), createSorter());
        this.paging(applyForms);
        this.setPageUrl(userType == 1 ? "sign/administration-out-apply-form.xhtml" : "public/administration-out-apply-form.xhtml");
        return "list";
    }

    @Action("/public/administration-out-apply-form-input")
    public String toAddByUser() throws Exception {
        userType = 2;
        return "add";
    }
    @Action("/sign/administration-out-apply-form-input")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/public/administration-out-apply-form-save")
    public String saveByUser() throws Exception {
        userType = 2;
        return save();
    }

    //保存
    @Action("/sign/administration-out-apply-form-save")
    public String save() throws Exception {
        if(applyForm != null){
            Long userId = getCurrentUserId();
            EmployeeinfoWork work = null;
            if(userId > 0){
                User user = userService.getUser(userId);
                if(user != null){
                    work = user.getWork();
                }
            }
            Date startDate = DateUtil.createDate(getParameter("s_start_date"), "yyyy-MM-dd HH:mm");
            Date endDate = DateUtil.createDate(getParameter("s_end_date"), "yyyy-MM-dd HH:mm");
            applyForm.setStartDate(startDate);
            applyForm.setEndDate(endDate);

            applyForm.setStatus(AuditStatus.wait_audit);
            applyForm.setWork(work); //如果是超级管理员操作，则work为null
            administrationOutApplyFormService.save(applyForm);
        }
        return execute();
    }

    @Action("/sign/administration-out-apply-form-audit")
    public String toAudit() throws Exception {
        //得到审核对象
        if(applyForm == null || applyForm.getId() == null){
            return execute();
        }
        applyForm = administrationOutApplyFormService.get(applyForm.getId());

        //得到当前审核历史记录(没有就先保存一个审核流程次数)
        audits = administrationOutApplyFormService.findAdministrationOutApplyFormAuditing(applyForm.getId());
        if(audits == null || audits.isEmpty()){
            administrationOutApplyFormService.saveAdministrationOutApplyFormAuditingProcess(applyForm.getId());
            audits = administrationOutApplyFormService.findAdministrationOutApplyFormAuditing(applyForm.getId());
        }
        //检查当前登录用户是否可以审核当前结点
        if(applyForm.getWork() != null && audits != null && !audits.isEmpty()){
            checkCanAudit(applyForm.getWork().getDuty(), audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_administration_out_apply_form.toString();

        return "audit";
    }

    public AdministrationOutApplyForm getApplyForm() {
        return applyForm;
    }

    public void setApplyForm(AdministrationOutApplyForm applyForm) {
        this.applyForm = applyForm;
    }

    public LimitedList<AdministrationOutApplyForm> getApplyForms() {
        return applyForms;
    }

    public List<List<AdministrationOutApplyFormAuditing>> getAudits() {
        return audits;
    }
}
